package cn.wgx.module.sys.services;

import cn.wgx.commons.util.StringUtils;
import cn.wgx.module.base.services.BaseService;
import cn.wgx.module.sys.entity.mapper .SysUserMapper;
import cn.wgx.module.sys.entity .SysUser;
import cn.wgx.module.sys.entity .SysUserExample;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SysUserService extends BaseService<SysUser> {

    @Resource
    SysUserMapper mapper;

    @Transactional(readOnly = true)
    public List<SysUser> getList(SysUser record, Boolean desc) {
		SysUserExample example = new SysUserExample();
		if(record != null){
            SysUserExample.Criteria criteria = example.createCriteria();
            if(StringUtils.isNotBlank(record.getUsername())){
                criteria.andUsernameEqualTo(record.getUsername());
            }
            if(StringUtils.isNotBlank(record.getPwd())){
                criteria.andPwdEqualTo(record.getPwd());
            }
        }
		example.setOrderByClause("createDate " + (desc?"desc": "asc"));
		return mapper.selectByExample(example);
    }

    public SysUser login(SysUser sysUser) {
        if(StringUtils.isBlank(sysUser.getUsername()) || StringUtils.isBlank(sysUser.getPwd())){
            return null;
        }
        List<SysUser> list = getList(sysUser, true);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Transactional(readOnly = true)
    public SysUser getOne(Integer id) {
		return mapper.selectByPrimaryKey(id);
    }

    @Transactional
    public SysUser insertOne( SysUser record) {
        mapper.insertSelective(record);
        return record;
    }


    @Transactional
    public SysUser updateOne( SysUser record) {
        mapper.updateByPrimaryKeySelective(record);
        return record;

    }
    @Transactional
    public int updatePass(SysUser record2) {
        SysUserExample example = new SysUserExample();
        example.createCriteria().andUsernameEqualTo(record2.getUsername());
        record2.setUsername(null);
        return mapper.updateByExampleSelective(record2, example);

    }

    @Transactional
    public int deleteOne( Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }


}